package com.zheng.wechat.mp.admin.controller.manage;

import com.zheng.common.base.BaseController;
import com.zheng.common.constant.Constant;
import com.zheng.common.util.DateUtil;
import com.zheng.common.util.JsonUtil;
import com.zheng.upms.dao.model.UpmsAgent;
import com.zheng.upms.dao.model.UpmsAgentExample;
import com.zheng.upms.dao.model.UpmsDict;
import com.zheng.upms.rpc.api.UpmsApiService;
import com.zheng.wechat.mp.common.constant.WechatMpConstant;
import com.zheng.wechat.mp.common.constant.WechatMpResult;
import com.zheng.wechat.mp.common.constant.WechatMpResultConstant;
import com.zheng.wechat.mp.dao.model.WechatMpGzuser;
import com.zheng.wechat.mp.dao.model.WechatMpGzuserExample;
import com.zheng.wechat.mp.rpc.api.WechatMpGzuserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/8.
 */
@Controller
@RequestMapping("/manage/gzuser")
@Api(value = "微信用户控制器", description = "微信用户控制器")
public class GzuserController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(GzuserController.class);

    @Autowired
    private WechatMpGzuserService wechatMpGzuserService;


    @Autowired
    private UpmsApiService upmsApiService;
    /**
     * 微信用户首页
     * @return
     */
    @ApiOperation(value = "微信用户首页")
    @RequiresPermissions("wechatmp:gzuser:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/gzuser/index.jsp";
    }


    @ApiOperation(value = "微信用户列表")
    @RequiresPermissions("wechatmp:gzuser:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order,
            @RequestParam(required = false,defaultValue = "0", value = "userId") int userId,
            @RequestParam(required = false, value = "userName") String userName,
            @RequestParam(required = false, value = "userTel") String userTel,
            @RequestParam(required = false, value = "beginTime") String beginTime,
            @RequestParam(required = false, value = "endTime") String endTime) {
        WechatMpGzuserExample wechatMpGzuserExample = new WechatMpGzuserExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            wechatMpGzuserExample.setOrderByClause(sort + " " + order);
        }
        WechatMpGzuserExample.Criteria criteria = wechatMpGzuserExample.createCriteria();
        if (StringUtils.isNotBlank(search)) {
            criteria.andNicknameLike("%" + search + "%");
        }
        if (0 < userId) {
            criteria.andIdEqualTo(userId);
        }
        if (StringUtils.isNotBlank(userName)) {
            criteria.andNicknameLike("%" + userName + "%");
        }
        if (StringUtils.isNotBlank(userTel)) {
            criteria.andMobileEqualTo(userTel);
        }
        if (StringUtils.isNotBlank(beginTime)) {
            criteria.andCreateTimeGreaterThanOrEqualTo(DateUtil.toDate(beginTime, DateUtil.FORMAT_YYYY_MM_DD));
        }
        if (StringUtils.isNotBlank(endTime)) {
            criteria.andCreateTimeLessThanOrEqualTo(DateUtil.toDate(endTime, DateUtil.FORMAT_YYYY_MM_DD));
        }
        //限制代理
        String loginUserName = getLoginUserName();
        Integer agentId = upmsApiService.selectUserAgentIdByUsername(loginUserName);
        if(agentId!=null && agentId!=Constant.CONST_SUPER_AGENTID){
            criteria.andUserAgentIdEqualTo(agentId);
        }

        List<WechatMpGzuser> rows = wechatMpGzuserService.selectByExampleForOffsetPage(wechatMpGzuserExample, offset, limit);
        long total = wechatMpGzuserService.countByExample(wechatMpGzuserExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "设置管理员页面")
    @RequiresPermissions("wechatmp:gzuser:manage")
    @RequestMapping(value = "/manage/{id}", method = {RequestMethod.GET})
    public String manage(@PathVariable("id") int id, ModelMap modelMap) {
        WechatMpGzuser wechatMpGzuser = wechatMpGzuserService.selectByPrimaryKey(id);
        modelMap.put("wechatMpGzuser",wechatMpGzuser);

        if(wechatMpGzuser.getAgentId()!=null){
            UpmsAgent upmsAgent = upmsApiService.selectUpmsAgentByPrimaryKey(wechatMpGzuser.getAgentId());
            modelMap.put("agent", upmsAgent);
        }
        UpmsAgentExample example = new UpmsAgentExample();
        example.createCriteria().andFlagEqualTo(Constant.CONST_FLAG_NORMAL);
        List<UpmsAgent> upmsAgents = upmsApiService.selectUpmsAgentByExample(example);
        modelMap.put("upmsAgents", JsonUtil.object2Json(upmsAgents));

        return "/manage/gzuser/manage.jsp";
    }

    @ApiOperation(value = "设置管理员")
    @RequiresPermissions("wechatmp:gzuser:manage")
    @RequestMapping(value = "/manage/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public Object manage(@PathVariable("id") int id, WechatMpGzuser wechatMpGzuser ) {
        wechatMpGzuser.setId(id);
        int count = wechatMpGzuserService.updateByPrimaryKeySelective(wechatMpGzuser);
        return new WechatMpResult(WechatMpResultConstant.SUCCESS, count);
    }
    @ApiOperation(value = "设置用户状态页面")
    @RequiresPermissions("wechatmp:gzuser:statusset")
    @RequestMapping(value = "/statusset/{id}", method = {RequestMethod.GET})
    public String statusset(@PathVariable("id") int id, ModelMap modelMap) {
        WechatMpGzuser wechatMpGzuser = wechatMpGzuserService.selectByPrimaryKey(id);
        modelMap.put("wechatMpGzuser",wechatMpGzuser);

        //用户状态
        List<UpmsDict> statusList = upmsApiService.selectUpmsDictByCode(WechatMpConstant.MP_USER_STATUS);
        modelMap.put("statusList", statusList);

        return "/manage/gzuser/statusset.jsp";
    }
    @ApiOperation(value = "锁定用户")
    @RequiresPermissions("wechatmp:gzuser:statusset")
    @RequestMapping(value = "/statusset/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public Object statusset(@PathVariable("id") int id, WechatMpGzuser wechatMpGzuser ) {
        wechatMpGzuser.setId(id);
        int count = wechatMpGzuserService.updateByPrimaryKeySelective(wechatMpGzuser);
        return new WechatMpResult(WechatMpResultConstant.SUCCESS, count);
    }
}

